﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class administrator_AddProducts : System.Web.UI.Page
{
    dulieuDataContext db=new dulieuDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
       if(!IsPostBack)
        {
          
            LoadNhomCha();
            LoadNhomCon();
           
        }
       FCK.Language = "vi";
       var fileBrowser = new CKFinder.FileBrowser();
       fileBrowser.BasePath = "../ckfinder/";
       fileBrowser.SetupCKEditor(FCK);
    }

    protected void LoadNhomCha()
    {
        drdnhomcha.DataSource = from o in db.Cates where o.TypeCateID == 2 && o.Parent == 0 select o;
        drdnhomcha.DataTextField = "Name";
        drdnhomcha.DataValueField = "Cate_ID";
        drdnhomcha.DataBind();
    }
    protected void LoadNhomCon()
    {
        drdnhomcon.DataSource = from o in db.Cates
                                where o.TypeCateID == 2 && o.Parent == Convert.ToInt32(drdnhomcha.SelectedValue)
                                select o;
        drdnhomcon.DataTextField = "Name";
        drdnhomcon.DataValueField = "Cate_ID";
        drdnhomcon.DataBind();
    }
    protected void imbtthem_Click(object sender, ImageClickEventArgs e)
    {
        var products = new Product
                           {
            Cate_ID = Convert.ToInt32(drdnhomcon.SelectedValue),
            Name = txttensp.Text,
            IDUser = 1,
            Descriptions = txttomtat.Text,Detail = FCK.Text,
            Image = GetFilePic(),
            Km = txtkhuyenmai.Text,
            Count = 1,
            Price = Convert.ToDouble(txtgiahang.Text),
            PriceOnline = Convert.ToDouble(txtgiaonline.Text),
            Quantity = Convert.ToInt16(txtsoluong.Text),
            Ensuarance = Convert.ToInt16(txtbaohanh.Text)
        };
        db.Products.InsertOnSubmit(products);
        db.SubmitChanges();
        lblthongbao.Text = "Thêm thành công";
    }
    public string GetFilePic()
    {
        var sfile = "";
        if (FileUpload1.HasFile)
        {
            sfile = Guid.NewGuid().ToString().Substring(0, 10) + FileUpload1.PostedFile.FileName.Remove(0, FileUpload1.PostedFile.FileName.LastIndexOf("."));
            FileUpload1.PostedFile.SaveAs(Server.MapPath("~/images/") + sfile);
        }
        return sfile;
    }
    protected void drdnhomcha_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadNhomCon();
    }
}